package com.example.handler;

import com.alibaba.fastjson2.JSON;
import com.example.domain.ResponseCode;
import com.example.domain.ResponseResult;
import com.example.utils.WebUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 授权失败处理器
 */
@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        ResponseResult result = new ResponseResult(
                ResponseCode.AUTHORIZATION_ERROR,
                "权限不足",
                null
        );
        // 处理异常
        WebUtil.renderString(response, JSON.toJSONString(result));
    }
}
